import 'package:casdon/common/service/tuya_service.dart';
import 'package:casdon/core/network/network_notifier.dart';
import 'package:casdon/core/network/network_state.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:tuya_home_sdk_flutter/tuya_home_sdk_flutter.dart';

class TuyaPairNetGuidNotifier
    extends NetworkNotifier<ThingSmartDeviceCategoryModel, String> {
  String? _pid; // 改为可空

  void setPid(String pid) {
    if (_pid == pid) return; // 相同pid不刷新
    _pid = pid;
    refresh();
  }

  @override
  Future<ThingSmartDeviceCategoryModel> fetchData(String params) async {
    if (params.isEmpty) throw Exception("PID不能为空");
    final model = await TuyaService().requestCategoryDetail(params, 1);
    if (model != null) return model;
    throw Exception("获取涂鸦categoryDetail失败，pid: $_pid");
  }

  @override
  String get params => _pid ?? ""; // 处理null情况
}

final tuyaPairNetGuidNotifierProvider =
    NotifierProvider<
      TuyaPairNetGuidNotifier,
      NetworkState<ThingSmartDeviceCategoryModel>
    >(TuyaPairNetGuidNotifier.new);
